//15.09.2005 tamer.oz Initial Creation
//15.09.2005 tamer.oz SetComboBox metodu eklendi
//15.09.2005 tamer.oz SetComboBox metodu eklendi
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using InfraStructureLayer;




/// <summary>
/// GUI Tarafinda kullanilacak genel metodlar
/// </summary>
/// 
namespace WebControls
{

    public class StaticMethods
    {
        public StaticMethods()
        {

        }


        public static List<Control> FindControlRecursive(Control parControl, string ctrlName)
        {
            List<Control> lst = new List<Control>();
            foreach (Control ctrl in parControl.Controls)
            {
                if (ctrl.ID == ctrlName)
                {
                    lst.Add(ctrl);
                    //return ctrl;
                }
                List<Control> retval = FindControlRecursive(ctrl, ctrlName);
                //if (retval != null)
                //{
                //    return retval;
                //}
                lst.AddRange(retval);
            }
            return lst;
        }

        public static string GetErrorMessageControlName(Control parControl)
        {
            if (parControl is ILabel)
            {
                List<SFLabel> lbl = GetLabel(parControl);
                if (lbl == null)
                {
                    return parControl.ID;
                }
                return lbl[0].Text;
            }
            else
            {
                return parControl.ID;
            }
        }
        public static string GetErrorMessageMultilanguageString(int Id,object[] parameters)
        {
            return String.Format(CurrentContext.ErrorMessagesMultilanguage[CurrentContext.LanguageId.ToString() + "|" + Id.ToString()].ToString(),parameters);
        }
        public static string GetMultilanguageString(Control parControl)
        {
            if (parControl is IMultiLanguage)
            {
                if (!((IMultiLanguage)parControl).IsProtectedForLanguageChange)
                {
                    if (((IMultiLanguage)parControl).IsMasterPageControl)
                    {
                        DataRow[] drArr = ((SFMasterPage)parControl.Page.Master).Multilanguage.Select("ControlId='" + parControl.ID + "'");
                        if (drArr.Length == 1)
                        {
                            return drArr[0]["ControlText"].ToString();
                        }
                    }
                    else
                    {
                        DataRow[] drArr = ((SFPage)parControl.Page).Multilanguage.Select("ControlId='" + parControl.ID + "'");
                        if (drArr.Length == 1)
                        {
                            return drArr[0]["ControlText"].ToString();
                        }
                    }
                    return "";
                }
                return "";
            }
            return "";
        }
        public static List<SFLabel> GetLabel(Control parControl)
        {
            if (parControl.Page == null)
            {
                return null;
            }
            if (parControl is ILabel)
            {
                if (parControl.Page.Master == null)
                {
                    if (((ILabel)parControl).ControlsLabel != null)
                    {
                        return StaticMethods.FindControlRecursive(parControl.Page, ((ILabel)parControl).ControlsLabel).ConvertAll<SFLabel>(new Converter<Control,SFLabel>(ControlToLabel));
                    }
                    return null;
                }
                else
                {
                    if (((ILabel)parControl).ControlsLabel != null)
                    {
                        return StaticMethods.FindControlRecursive(parControl.Page.Master, ((ILabel)parControl).ControlsLabel).ConvertAll<SFLabel>(new Converter<Control, SFLabel>(ControlToLabel));
                    }
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
        public static SFLabel ControlToLabel(Control ctrl)
        {
            return (SFLabel)ctrl;
        }

        public static string GetStringFromArray(string[] str)
        {
            string retval = "";
            foreach (string s in str)
            {
                retval += s + ",";
            }
            retval = retval.Remove(retval.Length - 1, 1);
            return retval;
        }
        public static DataTable FilterDataTable(DataTable parDataTable, string filter)
        {
            DataTable dt = parDataTable.Clone();
            foreach (DataRow dr in parDataTable.Select(filter))
            {
                dt.Rows.Add(dr.ItemArray);
            }
            return dt;
        }
        public static int GetIndexToDelete(DataTable parDataTable, int itemIndex)
        {

            for (int i = 0; i < parDataTable.Rows.Count; i++)
            {
                if (parDataTable.Rows[i].RowState == DataRowState.Deleted)
                {
                    itemIndex += 1;
                }
                if (itemIndex == i)
                {
                    break;
                }
            }
            return itemIndex;
        }

        public static void AddAttributes(WebControl ctrl)
        {
            ctrl.Attributes.Add("HasErrors", "false");
            ctrl.Attributes.Add("ErrorMessage", "");
            ctrl.Attributes.Add("JSValidationGroup", "");
        }


        public static DateTime AddWeekDays(DateTime date, int daysToAdd)
        {
            int daysAdded = 0;
            while (daysAdded < daysToAdd)
            {
                if (!(date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday))
                {
                    daysAdded++;
                }
                date = date.AddDays(1);
            }
            if (date.DayOfWeek == DayOfWeek.Saturday)
            {
                date = date.AddDays(2);
            }
            if (date.DayOfWeek == DayOfWeek.Sunday)
            {
                date = date.AddDays(1);
            }
            return date;
        }

    }
}
